package Yu;

import java.util.*;

public class Doubleball {
    public static int[] generateWinningNumbers() {
        int[] winningNumbers = new int[7];
        // 生成 6 个不重复的红色球号码
        Set<Integer> redBalls = new HashSet<>();
        Random random = new Random();
        while (redBalls.size() < 6) {
            int redBall = random.nextInt(33) + 1;
            redBalls.add(redBall);
        }
        int index = 0;
        for (int redBall : redBalls) {
            winningNumbers[index++] = redBall;
        }
        // 生成 1 个蓝色球号码
        winningNumbers[6] = random.nextInt(16) + 1;
        return winningNumbers;
    }

    // 获取用户输入的号码
    public static int[] getUserNumbers() {
        int[] userNumbers = new int[7];
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入 6 个不重复的红色球号码（1 - 33），用空格分隔：");
        Set<Integer> redBalls = new HashSet<>();
        while (redBalls.size() < 6) {
            try {
                String[] input = scanner.nextLine().split(" ");
                if (input.length != 6) {
                    System.out.println("输入的红色球号码数量不正确，请重新输入 6 个不重复的红色球号码（1 - 33），用空格分隔：");
                    continue;
                }
                for (String num : input) {
                    int redBall = Integer.parseInt(num);
                    if (redBall < 1 || redBall > 33) {
                        System.out.println("红色球号码超出范围，请重新输入 6 个不重复的红色球号码（1 - 33），用空格分隔：");
                        redBalls.clear();
                        break;
                    }
                    if (!redBalls.add(redBall)) {
                        System.out.println("红色球号码有重复，请重新输入 6 个不重复的红色球号码（1 - 33），用空格分隔：");
                        redBalls.clear();
                        break;
                    }
                }
            } catch (NumberFormatException e) {
                System.out.println("输入格式错误，请输入有效的数字，请重新输入 6 个不重复的红色球号码（1 - 33），用空格分隔：");
                redBalls.clear();
            }
        }
        int index = 0;
        for (int redBall : redBalls) {
            userNumbers[index++] = redBall;
        }
        System.out.println("请输入 1 个蓝色球号码（1 - 16）：");
        while (true) {
            try {
                int blueBall = scanner.nextInt();
                if (blueBall < 1 || blueBall > 16) {
                    System.out.println("蓝色球号码超出范围，请重新输入 1 个蓝色球号码（1 - 16）：");
                } else {
                    userNumbers[6] = blueBall;
                    break;
                }
            } catch (InputMismatchException e) {
                System.out.println("输入格式错误，请输入有效的数字，请重新输入 1 个蓝色球号码（1 - 16）：");
                scanner.nextInt();
            }
        }
        return userNumbers;
    }

    // 对比号码，计算命中球数和中奖级别
    public static void compareNumbers(int[] winningNumbers, int[] userNumbers) {
        int redHitCount = 0;
        boolean blueHit = false;
        // 计算红色球命中数
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (userNumbers[i] == winningNumbers[j]) {
                    redHitCount++;
                    break;
                }
            }
        }
        // 计算蓝色球是否命中
        blueHit = userNumbers[6] == winningNumbers[6];

        System.out.println("命中红球数：" + redHitCount);
        System.out.println("命中蓝球数：" + (blueHit ? 1 : 0));

        // 根据命中情况确定中奖级别
        String prizeLevel;
        if (redHitCount == 6 && blueHit) {
            prizeLevel = "一等奖";
        } else if (redHitCount == 6) {
            prizeLevel = "二等奖";
        } else if (redHitCount == 5 && blueHit) {
            prizeLevel = "三等奖";
        } else if ((redHitCount == 5) || (redHitCount == 4 && blueHit)) {
            prizeLevel = "四等奖";
        } else if ((redHitCount == 4) || (redHitCount == 3 && blueHit)) {
            prizeLevel = "五等奖";
        } else if (blueHit) {
            prizeLevel = "六等奖";
        } else {
            prizeLevel = "未中奖";
        }
        System.out.println("中奖级别：" + prizeLevel);
    }

    public static void main(String[] args) {
        // 生成中奖号码
        int[] winningNumbers = generateWinningNumbers();
        System.out.println("本期中奖号码已生成！");
        // 获取用户输入的号码
        int[] userNumbers = getUserNumbers();
        // 对比号码
        compareNumbers(winningNumbers, userNumbers);
    }
}

